<?php

session_start();
include './source/class/class_core.php';
if (!$_SESSION['name'])
	header('Location: index.php');
$name = $_SESSION['name'];
$user_id = $_SESSION['user_id'];

$date_time = strtotime(date('Y-m-d'));

$caidan = array();
require_once 'caidan.php';


if(isset($_POST['action']) & $_POST['action'] == 'save'){
	
	// 判断是否处于锁单状态
	$lock = file_get_contents('lock.php');
	if($lock == 1 & $_SESSION['name'] != 'MM'){
		header("Content-type: text/html; charset=utf-8");
		$msg = '提示：亲！管理员已经锁定今天的订单，请联系管理员追加订餐！';
		jump($msg, 'index.php');
	}
		
	
	
    $msg = '提示：订餐成功！';
    
    $num = $_POST['num'];
    $la = $_POST['la'];
    $remark = $_POST['remark'];
    
	$order_value = 0;
	
    if (is_array($num)){
	    foreach ($num as $k => $v){
	    	if ($v == 0){
	    		unset($num[$k]);
	    		unset($la[$k]);
	    	} else {
				$price = $caidan[$k]['price'];
				$order_info_ext[$k] = array('name' => $caidan[$k]['name'], 'price' => $price, 'num' => $v);
				$order_value += $price * $v;
			}
	    }
    }
    
	$order_info = array('num' => $num, 'la' => $la, 'remark' => $remark);
	
	if(@array_sum($num) == 0 && !isset($_GET['cancle'])){
		header("Content-type: text/html; charset=utf-8");
		$msg = '提示：你没有选定任何菜式 提交失败！';
		jump($msg, 'index.php');
	}
	
	
	$user_info = DB::fetch_first("SELECT money FROM `om_user` WHERE id = $user_id");
	
	
	
	
	// 入库
	$data_order = DB::fetch_first("SELECT id, order_info_ext FROM `om_order` WHERE order_index = '$date_time' AND user_id = '$user_id'");
	if( $data_order ){// 已经下单则需要执行更新就好了
		
		
		// 旧单总价
		foreach(unserialize($data_order['order_info_ext']) as $value){
			$data_order_money += $value['price'] * $value['num'];
		}
		
		/*
		// 余额判断
		if($order_value > $user_info['money'] + $data_order_money){
			$msg = '提示：你没有足够的余额支付此次订单，下单失败！';
			jump($msg, 'index.php');
		}
		*/
		
		DB::query("update om_user set money = money - ($order_value - $data_order_money) where id = $user_id");
		
		DB::update('order', array('order_info' => serialize($order_info), 'order_info_ext' => serialize($order_info_ext)), 'id = ' . $data_order['id']);
	} else {// 没有下过单，则插入一条新记录
		
		/*
		// 余额判断
		if($order_value > $user_info['money']){
			$msg = '提示：你没有足够的余额支付此次订单，下单失败！';
			jump($msg, 'index.php');
		}
		*/
		
		$data = array('order_index' => $date_time, 'user_id' => $user_id, 'name' => $name, 'order_info' => serialize($order_info), 'order_info_ext' => serialize($order_info_ext), 'order_time' => time());
		DB::insert('order', $data);
		
		DB::query("update om_user set money = money - $order_value where id = $user_id");
	}
	
	jump($msg, 'index.php');
}

if (isset($_GET['action']) & $_GET['action'] == 'cancle'){
	// 取消订单时补回金钱
	$data_order = DB::fetch_first("SELECT id, order_info_ext FROM `om_order` WHERE order_index = '$date_time' AND user_id = '$user_id'");
	// 旧单总价
	foreach(unserialize($data_order['order_info_ext']) as $value){
		$data_order_money += $value['price'] * $value['num'];
	}
	DB::query("update om_user set money = money + $data_order_money where id = $user_id");
	
	
	DB::delete('order', "order_index = '$date_time' AND id = '$user_id'");
	$msg = '提示：取消订单成功！';
	jump($msg, 'index.php');
}

function jump($msg, $url){
	header("Content-type: text/html; charset=utf-8");
	echo "<script>alert('$msg');window.location.href='$url';</script>";
	die;
}

